﻿using System.Collections.Generic;
using System.Web.Mvc;
using Journal.Common.Models;
using Journal.Common.Objects.Enums;
using Journal.Front.Presentation.Models.StudentModels;
using Journal.Front.Presentation.PresentationServicesInterfaces;

namespace Journal.Front.Presentation.Controllers
{
    public class StudentController : Controller
    {
        private readonly IStudentPresentationService studentPresentationService;

        public StudentController(IStudentPresentationService studentPresentationService)
        {
            this.studentPresentationService = studentPresentationService;
        }

        public ActionResult SelectPartial(int groupId)
        {
            StudentSelectModel model = studentPresentationService.GetStudentsListByStudyGroupId(groupId, GetUrlModel());
            return PartialView(model);
        }

        public ActionResult PopupAddStudent(int groupId)
        {
            StudentPartialEditModel model = studentPresentationService.CreateStudentPartialEditModel(groupId);
            return PartialView("PopupEditStudent", model);
        }

        public ActionResult PopupEditStudent(int studentId, int groupId)
        {
            StudentPartialEditModel model = studentPresentationService.GetStudentPartialEditModel(studentId);
            return PartialView(model);
        }

       
        public ActionResult PopupSaveStudent(StudentPartialEditModel studentPartialEditModel)
        {
            studentPresentationService.SavePartial(studentPartialEditModel);
            return RedirectToAction("SelectPartial", "Student", new { groupId = studentPartialEditModel.GroupId });
        }

        public ActionResult Delete(int studentId, int groupId)
        {
            studentPresentationService.Delete(studentId, groupId);
            return RedirectToAction("SelectPartial", "Student", new { groupId });
        }


        private UrlModel GetUrlModel()
        {
            IList<UrlModelItem> urls = new List<UrlModelItem>() 
            { 
                new UrlModelItem(x => Url.Action("PopupEditStudent", x), UrlType.Edit), 
                new UrlModelItem(x => Url.Action("Delete", x), UrlType.Delete),
            };
            return new UrlModel(urls);
        }
    }
}